package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.namespace.QName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.common.printer.client.Printer;
import com.ld.hpfs.common.printer.client.PrinterService;
import com.ld.hpfs.common.utils.Const;
import com.ld.hpfs.service.InvoiceBuyService;
import com.ld.hpfs.web.interseptor.WebConstants;
@Controller
public class StartPrinterController {
	private static final Logger logger = LoggerFactory.getLogger(StartPrinterController.class);
	
	@Autowired
	private InvoiceBuyService invoicebuyservice;

	private static  PrinterService services = null;
	private static final QName qName = new QName("http://service.printer.hps.com/", "PrinterService");
	
	/*
	 * 启动打印
	 * 
	 * fileByte：中间文件流
	 * printMode：打印方式  【0 预览打印；1 全自动打印； 2 半自动打印】
	 * previewFolio：预览张数  打印方式为半自动方式时该参数有效
	 * invCodeStart：起始发票代码
	 * invNoStart：起始发票号码
	 */
	public static boolean startPrinter(String clientIp, byte[] fileByte, int printMode, int previewFolio, String invCodeStart, String invNoStart) throws Exception {
		String clientIpPort = Const.PROPERTIES.getProperty("clientIpPort");
		if (clientIp == null) {
			throw new RuntimeException("Codeing error, PrinterCtrl must init(ip) before use.");
		}
		
		Printer port;				
			services = new PrinterService(new URL("http://"+clientIp +":"+clientIpPort+ "/IPrinter?wsdl"), qName);
			
			port = services.getPrinterPort();
		
		port.run(fileByte, printMode, previewFolio, invCodeStart, invNoStart);

		return true;
	}	

	/*
	 * 获取维护的打印机IP
	 */
	@RequestMapping(value = "/getPrinterIps")
	public void getPrinterIps(HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer){
		response.setContentType("text/plain;charset=UTF-8");
		HashMap<String, Object> param = new HashMap<String, Object>();
		ArrayList<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
		try {
			String companyId = session.getAttribute(WebConstants.CURRENT_COMPANY).toString();
			param.put("companyId", companyId);
			result = invoicebuyservice.getMachNames(param);
			
			List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
			HashMap<String, String> map1;
			for (int i = 0; i < result.size(); i++) {
				map1 = new HashMap<String, String>();
				map1.put("text", (String) result.get(i).get("name"));
				map1.put("value", (String) result.get(i).get("value"));
				list.add(map1);
			}
			
			String returnMsg = JackJson.fromObjectToJson(list);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}
}
